package com.yootk.listener;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebListener;

import java.util.EnumSet;

/**
 * 动态注册Filter组件
 */
@WebListener
public class DynamicRegistrationFilter implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) { // 容器初始化进行组件注册
        // 所有的组件的动态注册都是基于ServletContext接口实例完成的
        ServletContext application = sce.getServletContext(); // 获取上下文接口实例
        FilterRegistration.Dynamic filter = application.addFilter("baseFilter", "com.yootk.filter.BaseFilter");
        filter.setInitParameter("message", "www.yootk.com");
        filter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD), false, "/*");
    }
}
